<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template v-slot:kickback="{col,row}">
      <p class="kickback-list">
        <template v-for="v in level_list">
          <el-tag v-if="row[col.name] && typeof(row[col.name][v.id])!='undefined'">{{v.name}}：{{row[col.name][v.id]}}</el-tag>
        </template>
      </p>
    </template>
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
    </template>
  </list_url>
</template>
<script>
import {iframe, in_right_admin} from "@/js/tool.js";
import {_config, decode_config} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";
import {S94Model} from "@/js/S94Model.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    if (is_del){
      if(in_right_admin('KickbackRatio/restore')) btns['恢复'] = function(){ app.$refs['list_url'].restore() }
      if(in_right_admin('KickbackRatio/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }
    }else {
      if(in_right_admin('KickbackRatio/add')) btns['添加'] = function(){ iframe('kickback_ratio/edit') }
      if(in_right_admin('KickbackRatio/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }
    }
    var status_config = _config('status_alias_map.kickback_ratio.status');

    let model_list = [
      {label:'类目', name:'category'}
    ].concat(Object.values(_config('model_map')));
    let model_name_config = decode_config(model_list, {content_key:'label', value_key:'name'})
    let forms_config = {
      model_name: {label:'模型',name:'where[model_name][eq]',config:model_name_config,type:'select_one',value:''},
      object_id: {label:'项目id',name:'where[object_id][eq]',config:[],type:'text_one',value:''},
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', type: 'hidden', order:true},
      model_name: {name:'model_name', label:'模型', config:model_name_config, type: 'select_one', width:'100px'},
      object: {name:'object', label:'项目详情', min_width:'200px'},
    };
    list_cols = Object.assign(list_cols, {
      status: {name:'status', label:'状态', width:'70px',type:'select_one',config:status_config, fixed:'right'},
      action: {name:'action', label:'操作', width:'80px', type:'action', fixed:'right'},
    })

    return {
      config: {
        api: '/admin/kickback_ratio/'+(is_del?'recycle':'list'),
        title: "佣金比例列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          list.forEach(row=>{
            if (row.model_name){
              let model_label = S94Model.find(row.model_name)?.label;
              row.object = `【${model_label}】${row.object_title}`;
            }else {
              row.object = '默认';
            }
          })
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
      level_list: [],
      model_map: [],
    }
  },
  methods: {
    to_edit(row){
      let can_edit = in_right_admin('KickbackRatio/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      let param = {
        object_id: row.object_id,
        model_name: row.model_name || '',
        object_title: row.object_title,
      }
      if (!can_edit) param['is_info'] = 1;
      iframe('kickback_ratio/edit?'+s94Mjs.param_encode(param))
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    ajax({url:'/admin/kickback_ratio/config_map',cache:true}, function (res){
      let {user_level,kickback_type,model_list} = res.data;
      app.level_list = user_level;
      Object.keys(app.config.list_cols).forEach(function (k){
        if (/kickback_\d+/.test(k)) delete app.config.list_cols[k];
      });
      Object.values(kickback_type).forEach(v=>{
        let label = v.title + (!~[0,2].indexOf(v.type) ? '' : '(%)');
        let name = 'kickback_'+v.id;
        app.config.list_cols[name] = {name:name, label:label, width:'250px',type:'kickback'}
      });

    })
  }
}
</script>
